package com.football.springboot.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.football.springboot.entity.League;
import com.football.springboot.entity.Team;
import com.football.springboot.entity.TeamData;
import com.football.springboot.entity.TeamDataSingle;
import com.football.springboot.mapper.TeamDataMapper;
import com.football.springboot.service.IGameService;
import com.football.springboot.service.ILeagueService;
import com.football.springboot.service.ITeamDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.football.springboot.common.KeepTwoDecimal;

@Service
public class TeamDataServiceImpl extends ServiceImpl<TeamDataMapper, TeamData> implements ITeamDataService {


    @Resource
    private IGameService gameService;

    @Resource
    private TeamDataMapper teamDataMapper;

    @Resource
    private ILeagueService leagueService;
    @Override
    public TeamData addgamedata(TeamDataSingle teamDataSingle) {
        //新增球队比赛数据时添加该球队的赛季数据(从0到1)
        TeamData teamData = new TeamData();
        //设置主场球队id
        teamData.setTeamId(teamDataSingle.getTeamId());
        //设置该条数据参加的联赛id (根据gameId去查询比赛信息就能知道是哪个联赛的)
        teamData.setLeagueId(gameService.getLeagueinfo(teamDataSingle.getGameId()).getId());
        //设置该条数据参加的联赛赛季（跟上同理，查出联赛信息即可）
        teamData.setLeagueSeason(gameService.getLeagueinfo(teamDataSingle.getGameId()).getLeagueSeason());
        //设置已进行的轮数，如果是新增，那肯定是第一轮(在sevice里可以进行判断)
        teamData.setTotalRounds(1);

        //设置胜平负场数（if逻辑判断）和积分
        //先都设置为0
        teamData.setTotalWin(0);
        teamData.setTotalFlatten(0);
        teamData.setTotalLose(0);
        teamData.setTotalPoints(0.0);
        teamData.setTotalGoal(0.0);
        teamData.setTotalFumble(0.0);
        Double singleGoal1 = teamDataSingle.getSingleGoal();
        Double singleFumble1 = teamDataSingle.getSingleFumble();
        BigDecimal singleGoal = new BigDecimal(singleGoal1);
        BigDecimal singleFumble = new BigDecimal(singleFumble1);
        if (singleGoal.compareTo(singleFumble)>0){
            teamData.setTotalWin(1);
            teamData.setTotalPoints(3.0);
        }else if (singleGoal.compareTo(singleFumble)==0){
            teamData.setTotalFlatten(1);
            teamData.setTotalPoints(1.0);
        }else{
            teamData.setTotalLose(1);
            teamData.setTotalPoints(0.0);
        }
        //设置其余总参数
        teamData.setTotalGoal(teamDataSingle.getSingleGoal());
        teamData.setTotalFumble(teamDataSingle.getSingleFumble());
        teamData.setTotalControl(teamDataSingle.getSingleControl());
        teamData.setTotalShoot(teamDataSingle.getSingleShoot());
        teamData.setTotalOntarget(teamDataSingle.getSingleOntarget());
        teamData.setTotalPass(teamDataSingle.getSinglePass());
        teamData.setTotalOkpass(teamDataSingle.getSingleOkpass());
        teamData.setTotalSurpass(teamDataSingle.getSingleSurpass());
        teamData.setTotalOffside(teamDataSingle.getSingleOffside());
        teamData.setTotalFoul(teamDataSingle.getSingleFoul());
        teamData.setTotalYcard(teamDataSingle.getSingleYcard());
        teamData.setTotalRcard(teamDataSingle.getSingleRcard());


        return teamData;
    }

    @Override
    public TeamData getupdategamedata(Integer teamId, Integer leagueId,String homeAway) {
        return teamDataMapper.getupdategamedata(teamId,leagueId,homeAway);
    }


    @Override
    public TeamData updategamedata(TeamData teamData,TeamDataSingle teamDataSingle) {
        //新增球队的赛季比赛数据（比赛数从1到n,第一次新增第二轮以上的比赛数据时）
        //先找到要更新的那个hometeamData 根据teamid、leagueid查询出来
        Double singleGoal1 = teamDataSingle.getSingleGoal();
        Double singleFumble1 = teamDataSingle.getSingleFumble();
        BigDecimal singleGoal = new BigDecimal(singleGoal1);
        BigDecimal singleFumble = new BigDecimal(singleFumble1);

        //设置胜平负场数和积分
        if (singleGoal.compareTo(singleFumble)>0){
            teamData.setTotalWin(teamData.getTotalWin()+1);
            teamData.setTotalPoints(teamData.getTotalPoints()+3);
        }else if (singleGoal.compareTo(singleFumble)==0){
            teamData.setTotalFlatten(teamData.getTotalFlatten()+1);
            teamData.setTotalPoints(teamData.getTotalPoints()+1);
        }else{
            teamData.setTotalLose(teamData.getTotalLose()+1);
            teamData.setTotalPoints(teamData.getTotalPoints()+0);
        }

        //设置已经进行的轮数（这里因为我们是按照规则添加比赛的，所以就按照我们输入的轮数来就行了
        // 或者应该是3个字段 胜平负三场加起来）
        //teamData.setTotalRounds(gameService.getById(teamDataSingle.getGameId()).getGameRounds());
        teamData.setTotalRounds(teamData.getTotalWin()+teamData.getTotalFlatten()+teamData.getTotalLose());


        //设置其余参数
        teamData.setTotalGoal(teamData.getTotalGoal()+teamDataSingle.getSingleGoal());
        teamData.setTotalFumble(teamData.getTotalFumble()+teamDataSingle.getSingleFumble());

        System.err.println("总控球："+(teamData.getTotalControl()*(teamData.getTotalRounds()-1)+teamDataSingle.getSingleControl()));

        System.err.println("场次数："+Double.valueOf(teamData.getTotalRounds()));

        teamData.setTotalControl((teamData.getTotalControl()*(teamData.getTotalRounds()-1)+teamDataSingle.getSingleControl())/Double.valueOf(teamData.getTotalRounds()));
        BigDecimal two = new BigDecimal(teamData.getTotalControl());
        double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        teamData.setTotalControl(three);
        System.err.println("平均控球："+teamData.getTotalControl());

        teamData.setTotalShoot(teamData.getTotalShoot()+teamDataSingle.getSingleShoot());
        teamData.setTotalOntarget(teamData.getTotalOntarget()+teamDataSingle.getSingleOntarget());
        teamData.setTotalPass(teamData.getTotalPass()+teamDataSingle.getSinglePass());
        teamData.setTotalOkpass(teamData.getTotalOkpass()+teamDataSingle.getSingleOkpass());
        teamData.setTotalSurpass(teamData.getTotalSurpass()+teamDataSingle.getSingleSurpass());
        teamData.setTotalOffside(teamData.getTotalOffside()+teamDataSingle.getSingleOffside());
        teamData.setTotalFoul(teamData.getTotalFoul()+teamDataSingle.getSingleFoul());
        teamData.setTotalYcard(teamData.getTotalYcard()+teamDataSingle.getSingleYcard());
        teamData.setTotalRcard(teamData.getTotalRcard()+teamDataSingle.getSingleRcard());

        return teamData;
    }

    @Override
    public TeamData updategamedataexist(TeamData teamData, TeamDataSingle teamDataSinglebefore,TeamDataSingle teamDataSingle) {
        //修改球队的赛季比赛数据(当你修改已经添加的比赛数据时)

        //未修改之前的单场比赛数据
        Double singlecontrolbefore = teamDataSinglebefore.getSingleControl();
        Double singleshootbefore = teamDataSinglebefore.getSingleShoot();
        Double singleontargetbefore = teamDataSinglebefore.getSingleOntarget();
        Double singlepassbefore = teamDataSinglebefore.getSinglePass();
        Double singleokpassbefore = teamDataSinglebefore.getSingleOkpass();
        Double singlesurpassbefore = teamDataSinglebefore.getSingleSurpass();
        Double singleoffsidebefore = teamDataSinglebefore.getSingleOffside();
        Double singlefoulbefore = teamDataSinglebefore.getSingleFoul();
        Double singleycardbefore = teamDataSinglebefore.getSingleYcard();
        Double singlercardbefore = teamDataSinglebefore.getSingleRcard();

        //修改之后的单场比赛数据
        Double singlecontrol = teamDataSingle.getSingleControl();
        Double singleshoot = teamDataSingle.getSingleShoot();
        Double singleontarget = teamDataSingle.getSingleOntarget();
        Double singlepass = teamDataSingle.getSinglePass();
        Double singleokpass = teamDataSingle.getSingleOkpass();
        Double singlesurpass = teamDataSingle.getSingleSurpass();
        Double singleoffside = teamDataSingle.getSingleOffside();
        Double singlefoul = teamDataSingle.getSingleFoul();
        Double singleycard = teamDataSingle.getSingleYcard();
        Double singlercard = teamDataSingle.getSingleRcard();


        Double singleGoalbefore1 = teamDataSinglebefore.getSingleGoal();
        Double singleFumblebefore1 = teamDataSinglebefore.getSingleFumble();
        BigDecimal singleGoalbefore = new BigDecimal(singleGoalbefore1);
        BigDecimal singleFumblebefore = new BigDecimal(singleFumblebefore1);

        Double singleGoal1 = teamDataSingle.getSingleGoal();
        Double singleFumble1 = teamDataSingle.getSingleFumble();
        BigDecimal singleGoal = new BigDecimal(singleGoal1);
        BigDecimal singleFumble = new BigDecimal(singleFumble1);

        //有9种胜负变更关系
//        before     now
//        胜	     胜
//        胜	     平
//        胜	     负
//        平	     胜
//        平	     平
//        平	     负
//        负	     胜
//        负	     平
//        负	     负
        //根据胜负关系的变更  更新胜平负场数和积分
        if(singleGoalbefore.compareTo(singleFumblebefore)>0 && singleGoal.compareTo(singleFumble)>0){ //胜 胜
            //胜负平和积分都保持不变 不用设置了 其他的内容进行更新
            teamData.setTotalGoal(teamData.getTotalGoal()-singleGoalbefore1+singleGoal1);
            teamData.setTotalFumble(teamData.getTotalFumble()-singleFumblebefore1+singleFumble1);
            teamData.setTotalControl((teamData.getTotalControl()*teamData.getTotalRounds()-singlecontrolbefore+singlecontrol)/Double.valueOf(teamData.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamData.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamData.setTotalControl(three);
            teamData.setTotalShoot(teamData.getTotalShoot()-singleshootbefore+singleshoot);
            teamData.setTotalOntarget(teamData.getTotalOntarget()-singleontargetbefore+singleontarget);
            teamData.setTotalPass(teamData.getTotalPass()-singlepassbefore+singlepass);
            teamData.setTotalOkpass(teamData.getTotalOkpass()-singleokpassbefore+singleokpass);
            teamData.setTotalSurpass(teamData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
            teamData.setTotalOffside(teamData.getTotalOffside()-singleoffsidebefore+singleoffside);
            teamData.setTotalFoul(teamData.getTotalFoul()-singlefoulbefore+singlefoul);
            teamData.setTotalYcard(teamData.getTotalYcard()-singleycardbefore+singleycard);
            teamData.setTotalRcard(teamData.getTotalRcard()-singlercardbefore+singlercard);


        }else if(singleGoalbefore.compareTo(singleFumblebefore)>0 && singleGoal.compareTo(singleFumble)==0){ //胜 平
            //胜场-1 平场+1 积分-2
            teamData.setTotalWin(teamData.getTotalWin()-1);
            teamData.setTotalFlatten(teamData.getTotalFlatten()+1);
            teamData.setTotalPoints(teamData.getTotalPoints()-2);
            teamData.setTotalGoal(teamData.getTotalGoal()-singleGoalbefore1+singleGoal1);
            teamData.setTotalFumble(teamData.getTotalFumble()-singleFumblebefore1+singleFumble1);
            teamData.setTotalControl((teamData.getTotalControl()*teamData.getTotalRounds()-singlecontrolbefore+singlecontrol)/Double.valueOf(teamData.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamData.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamData.setTotalControl(three);
            teamData.setTotalShoot(teamData.getTotalShoot()-singleshootbefore+singleshoot);
            teamData.setTotalOntarget(teamData.getTotalOntarget()-singleontargetbefore+singleontarget);
            teamData.setTotalPass(teamData.getTotalPass()-singlepassbefore+singlepass);
            teamData.setTotalOkpass(teamData.getTotalOkpass()-singleokpassbefore+singleokpass);
            teamData.setTotalSurpass(teamData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
            teamData.setTotalOffside(teamData.getTotalOffside()-singleoffsidebefore+singleoffside);
            teamData.setTotalFoul(teamData.getTotalFoul()-singlefoulbefore+singlefoul);
            teamData.setTotalYcard(teamData.getTotalYcard()-singleycardbefore+singleycard);
            teamData.setTotalRcard(teamData.getTotalRcard()-singlercardbefore+singlercard);

        }else if(singleGoalbefore.compareTo(singleFumblebefore)>0 && singleGoal.compareTo(singleFumble)<0) {//胜 负
            //胜场-1 负场+1 积分-3
            teamData.setTotalWin(teamData.getTotalWin()-1);
            teamData.setTotalLose(teamData.getTotalLose()+1);
            teamData.setTotalPoints(teamData.getTotalPoints()-3);
            teamData.setTotalGoal(teamData.getTotalGoal()-singleGoalbefore1+singleGoal1);
            teamData.setTotalFumble(teamData.getTotalFumble()-singleFumblebefore1+singleFumble1);
            teamData.setTotalControl((teamData.getTotalControl()*teamData.getTotalRounds()-singlecontrolbefore+singlecontrol)/Double.valueOf(teamData.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamData.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamData.setTotalControl(three);
            teamData.setTotalShoot(teamData.getTotalShoot()-singleshootbefore+singleshoot);
            teamData.setTotalOntarget(teamData.getTotalOntarget()-singleontargetbefore+singleontarget);
            teamData.setTotalPass(teamData.getTotalPass()-singlepassbefore+singlepass);
            teamData.setTotalOkpass(teamData.getTotalOkpass()-singleokpassbefore+singleokpass);
            teamData.setTotalSurpass(teamData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
            teamData.setTotalOffside(teamData.getTotalOffside()-singleoffsidebefore+singleoffside);
            teamData.setTotalFoul(teamData.getTotalFoul()-singlefoulbefore+singlefoul);
            teamData.setTotalYcard(teamData.getTotalYcard()-singleycardbefore+singleycard);
            teamData.setTotalRcard(teamData.getTotalRcard()-singlercardbefore+singlercard);

        }else if(singleGoalbefore.compareTo(singleFumblebefore)==0 && singleGoal.compareTo(singleFumble)>0) {//平 胜
            //平场-1 胜场+1 积分+2
            teamData.setTotalFlatten(teamData.getTotalFlatten()-1);
            teamData.setTotalWin(teamData.getTotalWin()+1);
            teamData.setTotalPoints(teamData.getTotalPoints()+2);
            teamData.setTotalGoal(teamData.getTotalGoal()-singleGoalbefore1+singleGoal1);
            teamData.setTotalFumble(teamData.getTotalFumble()-singleFumblebefore1+singleFumble1);
            teamData.setTotalControl((teamData.getTotalControl()*teamData.getTotalRounds()-singlecontrolbefore+singlecontrol)/Double.valueOf(teamData.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamData.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamData.setTotalControl(three);
            teamData.setTotalShoot(teamData.getTotalShoot()-singleshootbefore+singleshoot);
            teamData.setTotalOntarget(teamData.getTotalOntarget()-singleontargetbefore+singleontarget);
            teamData.setTotalPass(teamData.getTotalPass()-singlepassbefore+singlepass);
            teamData.setTotalOkpass(teamData.getTotalOkpass()-singleokpassbefore+singleokpass);
            teamData.setTotalSurpass(teamData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
            teamData.setTotalOffside(teamData.getTotalOffside()-singleoffsidebefore+singleoffside);
            teamData.setTotalFoul(teamData.getTotalFoul()-singlefoulbefore+singlefoul);
            teamData.setTotalYcard(teamData.getTotalYcard()-singleycardbefore+singleycard);
            teamData.setTotalRcard(teamData.getTotalRcard()-singlercardbefore+singlercard);

        }else if (singleGoalbefore.compareTo(singleFumblebefore)==0 && singleGoal.compareTo(singleFumble)==0){//平 平
                //胜负平和积分都保持不变 不用设置了 其他的内容进行更新
            teamData.setTotalGoal(teamData.getTotalGoal()-singleGoalbefore1+singleGoal1);
            teamData.setTotalFumble(teamData.getTotalFumble()-singleFumblebefore1+singleFumble1);
            teamData.setTotalControl((teamData.getTotalControl()*teamData.getTotalRounds()-singlecontrolbefore+singlecontrol)/Double.valueOf(teamData.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamData.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamData.setTotalControl(three);
            teamData.setTotalShoot(teamData.getTotalShoot()-singleshootbefore+singleshoot);
            teamData.setTotalOntarget(teamData.getTotalOntarget()-singleontargetbefore+singleontarget);
            teamData.setTotalPass(teamData.getTotalPass()-singlepassbefore+singlepass);
            teamData.setTotalOkpass(teamData.getTotalOkpass()-singleokpassbefore+singleokpass);
            teamData.setTotalSurpass(teamData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
            teamData.setTotalOffside(teamData.getTotalOffside()-singleoffsidebefore+singleoffside);
            teamData.setTotalFoul(teamData.getTotalFoul()-singlefoulbefore+singlefoul);
            teamData.setTotalYcard(teamData.getTotalYcard()-singleycardbefore+singleycard);
            teamData.setTotalRcard(teamData.getTotalRcard()-singlercardbefore+singlercard);

        }else if (singleGoalbefore.compareTo(singleFumblebefore)==0 && singleGoal.compareTo(singleFumble)<0){//平 负
            //平场-1 负场+1 积分-1
            teamData.setTotalFlatten(teamData.getTotalFlatten()-1);
            teamData.setTotalLose(teamData.getTotalLose()+1);
            teamData.setTotalPoints(teamData.getTotalPoints()-1);
            teamData.setTotalGoal(teamData.getTotalGoal()-singleGoalbefore1+singleGoal1);
            teamData.setTotalFumble(teamData.getTotalFumble()-singleFumblebefore1+singleFumble1);
            teamData.setTotalControl((teamData.getTotalControl()*teamData.getTotalRounds()-singlecontrolbefore+singlecontrol)/Double.valueOf(teamData.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamData.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamData.setTotalControl(three);
            teamData.setTotalShoot(teamData.getTotalShoot()-singleshootbefore+singleshoot);
            teamData.setTotalOntarget(teamData.getTotalOntarget()-singleontargetbefore+singleontarget);
            teamData.setTotalPass(teamData.getTotalPass()-singlepassbefore+singlepass);
            teamData.setTotalOkpass(teamData.getTotalOkpass()-singleokpassbefore+singleokpass);
            teamData.setTotalSurpass(teamData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
            teamData.setTotalOffside(teamData.getTotalOffside()-singleoffsidebefore+singleoffside);
            teamData.setTotalFoul(teamData.getTotalFoul()-singlefoulbefore+singlefoul);
            teamData.setTotalYcard(teamData.getTotalYcard()-singleycardbefore+singleycard);
            teamData.setTotalRcard(teamData.getTotalRcard()-singlercardbefore+singlercard);

        }else if (singleGoalbefore.compareTo(singleFumblebefore)<0 && singleGoal.compareTo(singleFumble)>0){//负 胜
            //负场-1 胜场+1 积分+3
            teamData.setTotalLose(teamData.getTotalLose()-1);
            teamData.setTotalWin(teamData.getTotalWin()+1);
            teamData.setTotalPoints(teamData.getTotalPoints()+3);
            teamData.setTotalGoal(teamData.getTotalGoal()-singleGoalbefore1+singleGoal1);
            teamData.setTotalFumble(teamData.getTotalFumble()-singleFumblebefore1+singleFumble1);
            teamData.setTotalControl((teamData.getTotalControl()*teamData.getTotalRounds()-singlecontrolbefore+singlecontrol)/Double.valueOf(teamData.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamData.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamData.setTotalControl(three);
            teamData.setTotalShoot(teamData.getTotalShoot()-singleshootbefore+singleshoot);
            teamData.setTotalOntarget(teamData.getTotalOntarget()-singleontargetbefore+singleontarget);
            teamData.setTotalPass(teamData.getTotalPass()-singlepassbefore+singlepass);
            teamData.setTotalOkpass(teamData.getTotalOkpass()-singleokpassbefore+singleokpass);
            teamData.setTotalSurpass(teamData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
            teamData.setTotalOffside(teamData.getTotalOffside()-singleoffsidebefore+singleoffside);
            teamData.setTotalFoul(teamData.getTotalFoul()-singlefoulbefore+singlefoul);
            teamData.setTotalYcard(teamData.getTotalYcard()-singleycardbefore+singleycard);
            teamData.setTotalRcard(teamData.getTotalRcard()-singlercardbefore+singlercard);

        }else if (singleGoalbefore.compareTo(singleFumblebefore)<0 && singleGoal.compareTo(singleFumble)==0){//负 平
            //负场-1 平场+1 积分+1
            teamData.setTotalLose(teamData.getTotalLose()-1);
            teamData.setTotalFlatten(teamData.getTotalFlatten()+1);
            teamData.setTotalPoints(teamData.getTotalPoints()+1);
            teamData.setTotalGoal(teamData.getTotalGoal()-singleGoalbefore1+singleGoal1);
            teamData.setTotalFumble(teamData.getTotalFumble()-singleFumblebefore1+singleFumble1);
            teamData.setTotalControl((teamData.getTotalControl()*teamData.getTotalRounds()-singlecontrolbefore+singlecontrol)/Double.valueOf(teamData.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamData.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamData.setTotalControl(three);
            teamData.setTotalShoot(teamData.getTotalShoot()-singleshootbefore+singleshoot);
            teamData.setTotalOntarget(teamData.getTotalOntarget()-singleontargetbefore+singleontarget);
            teamData.setTotalPass(teamData.getTotalPass()-singlepassbefore+singlepass);
            teamData.setTotalOkpass(teamData.getTotalOkpass()-singleokpassbefore+singleokpass);
            teamData.setTotalSurpass(teamData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
            teamData.setTotalOffside(teamData.getTotalOffside()-singleoffsidebefore+singleoffside);
            teamData.setTotalFoul(teamData.getTotalFoul()-singlefoulbefore+singlefoul);
            teamData.setTotalYcard(teamData.getTotalYcard()-singleycardbefore+singleycard);
            teamData.setTotalRcard(teamData.getTotalRcard()-singlercardbefore+singlercard);

        }else if (singleGoalbefore.compareTo(singleFumblebefore)<0 && singleGoal.compareTo(singleFumble)<0){//负 负
                //胜负平和积分都保持不变 不用设置了 其他的内容进行更新
            teamData.setTotalGoal(teamData.getTotalGoal()-singleGoalbefore1+singleGoal1);
            teamData.setTotalFumble(teamData.getTotalFumble()-singleFumblebefore1+singleFumble1);
            teamData.setTotalControl((teamData.getTotalControl()*teamData.getTotalRounds()-singlecontrolbefore+singlecontrol)/Double.valueOf(teamData.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamData.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamData.setTotalControl(three);
            teamData.setTotalShoot(teamData.getTotalShoot()-singleshootbefore+singleshoot);
            teamData.setTotalOntarget(teamData.getTotalOntarget()-singleontargetbefore+singleontarget);
            teamData.setTotalPass(teamData.getTotalPass()-singlepassbefore+singlepass);
            teamData.setTotalOkpass(teamData.getTotalOkpass()-singleokpassbefore+singleokpass);
            teamData.setTotalSurpass(teamData.getTotalSurpass()-singlesurpassbefore+singlesurpass);
            teamData.setTotalOffside(teamData.getTotalOffside()-singleoffsidebefore+singleoffside);
            teamData.setTotalFoul(teamData.getTotalFoul()-singlefoulbefore+singlefoul);
            teamData.setTotalYcard(teamData.getTotalYcard()-singleycardbefore+singleycard);
            teamData.setTotalRcard(teamData.getTotalRcard()-singlercardbefore+singlercard);

        }

        return teamData;
    }

    @Override
    public List<TeamData> getAllTeamData() {
        return teamDataMapper.getAllTeamData();
    }

    @Override
    public Team getteaminfo(Integer teamId) {
        return teamDataMapper.getteaminfo(teamId);
    }

    @Override
    public League getleagueinfo(Integer leagueId) {
        return teamDataMapper.getleagueinfo(leagueId);
    }

    @Override
    public void countleague(TeamData teamData) {
        KeepTwoDecimal keepTwoDecimal=new KeepTwoDecimal();
        teamData.setGoalDiff(teamData.getTotalGoal()-teamData.getTotalFumble());
        teamData.setAverageGoal(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalGoal()/teamData.getTotalRounds()));
        teamData.setAverageFumble(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalFumble()/teamData.getTotalRounds()));
        teamData.setAverageShoot(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalShoot()/teamData.getTotalRounds()));
        teamData.setAverageOntarget(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalOntarget()/teamData.getTotalRounds()));
        teamData.setAveragePass(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalPass()/teamData.getTotalRounds()));
        teamData.setAverageOkpass(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalOkpass()/teamData.getTotalRounds()));
        teamData.setAverageSurpass(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalSurpass()/teamData.getTotalRounds()));
        teamData.setAverageOffside(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalOffside()/teamData.getTotalRounds()));
        teamData.setAverageFoul(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalFoul()/teamData.getTotalRounds()));
        teamData.setAverageYcard(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalYcard()/teamData.getTotalRounds()));
        teamData.setAverageRcard(keepTwoDecimal.KeepTwoDecimal(teamData.getTotalRcard()/teamData.getTotalRounds()));
    }

    @Override
    public TeamData teamDataall(TeamData hometeamdata, TeamData awayteamdata) {
        TeamData teamDataall = new TeamData();

            //主客场数据都存在的时候相加
            teamDataall.setLeagueId(hometeamdata.getLeagueId());
            teamDataall.setLeagueSeason(leagueService.getById(hometeamdata.getLeagueId()).getLeagueSeason());
            teamDataall.setHomeAway("全部");
            teamDataall.setTeamId(hometeamdata.getTeamId());
            teamDataall.setTotalRounds(hometeamdata.getTotalRounds() + awayteamdata.getTotalRounds());
            teamDataall.setTotalWin(hometeamdata.getTotalWin() + awayteamdata.getTotalWin());
            teamDataall.setTotalFlatten(hometeamdata.getTotalFlatten() + awayteamdata.getTotalFlatten());
            teamDataall.setTotalLose(hometeamdata.getTotalLose() + awayteamdata.getTotalLose());
            teamDataall.setTotalGoal(hometeamdata.getTotalGoal() + awayteamdata.getTotalGoal());
            teamDataall.setTotalFumble(hometeamdata.getTotalFumble() + awayteamdata.getTotalFumble());
            teamDataall.setTotalControl((hometeamdata.getTotalControl() + awayteamdata.getTotalControl()) / 2);
            teamDataall.setTotalShoot(hometeamdata.getTotalShoot() + awayteamdata.getTotalShoot());
            teamDataall.setTotalOntarget(hometeamdata.getTotalOntarget() + awayteamdata.getTotalOntarget());
            teamDataall.setTotalPass(hometeamdata.getTotalPass() + awayteamdata.getTotalPass());
            teamDataall.setTotalOkpass(hometeamdata.getTotalOkpass() + awayteamdata.getTotalOkpass());
            teamDataall.setTotalSurpass(hometeamdata.getTotalSurpass() + awayteamdata.getTotalSurpass());
            teamDataall.setTotalOffside(hometeamdata.getTotalOffside() + awayteamdata.getTotalOffside());
            teamDataall.setTotalFoul(hometeamdata.getTotalFoul() + awayteamdata.getTotalFoul());
            teamDataall.setTotalYcard(hometeamdata.getTotalYcard() + awayteamdata.getTotalYcard());
            teamDataall.setTotalRcard(hometeamdata.getTotalRcard() + awayteamdata.getTotalRcard());
            teamDataall.setTotalPoints(hometeamdata.getTotalPoints() + awayteamdata.getTotalPoints());
            countleague(teamDataall);
            return teamDataall;
        }

    @Override
    public TeamData teamDataallleague(List<TeamData> teamDataList, TeamData teamDataall) {
        teamDataall.setTotalRounds(0);
        teamDataall.setTotalWin(0);
        teamDataall.setTotalFlatten(0);
        teamDataall.setTotalLose(0);
        teamDataall.setTotalGoal(0.0);
        teamDataall.setTotalFumble(0.0);
        teamDataall.setTotalControl(0.0);
        teamDataall.setTotalShoot(0.0);
        teamDataall.setTotalOntarget(0.0);
        teamDataall.setTotalPass(0.0);
        teamDataall.setTotalOkpass(0.0);
        teamDataall.setTotalSurpass(0.0);
        teamDataall.setTotalOffside(0.0);
        teamDataall.setTotalFoul(0.0);
        teamDataall.setTotalYcard(0.0);
        teamDataall.setTotalRcard(0.0);
        teamDataall.setTotalPoints(0.0);
        for (int i=0;i<teamDataList.size();i++){
            teamDataall.setTeamId(teamDataall.getTeamId());
            teamDataall.setTotalRounds(teamDataall.getTotalRounds() + 1);
            teamDataall.setTotalWin(teamDataall.getTotalWin() + teamDataList.get(i).getTotalWin());
            teamDataall.setTotalFlatten(teamDataall.getTotalFlatten() + teamDataList.get(i).getTotalFlatten());
            teamDataall.setTotalLose(teamDataall.getTotalLose() + teamDataList.get(i).getTotalLose());
            teamDataall.setTotalGoal(teamDataall.getTotalGoal() + teamDataList.get(i).getTotalGoal());
            teamDataall.setTotalFumble(teamDataall.getTotalFumble() + teamDataList.get(i).getTotalFumble());

            teamDataall.setTotalControl((teamDataall.getTotalControl()*(teamDataall.getTotalRounds()-1) + teamDataList.get(i).getTotalControl()) / Double.valueOf(teamDataall.getTotalRounds()));
            BigDecimal two = new BigDecimal(teamDataall.getTotalControl());
            double three = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            teamDataall.setTotalControl(three);
            teamDataall.setTotalShoot(teamDataall.getTotalShoot() + teamDataList.get(i).getTotalShoot());
            teamDataall.setTotalOntarget(teamDataall.getTotalOntarget() + teamDataList.get(i).getTotalOntarget());
            teamDataall.setTotalPass(teamDataall.getTotalPass() + teamDataList.get(i).getTotalPass());
            teamDataall.setTotalOkpass(teamDataall.getTotalOkpass() + teamDataList.get(i).getTotalOkpass());
            teamDataall.setTotalSurpass(teamDataall.getTotalSurpass() + teamDataList.get(i).getTotalSurpass());
            teamDataall.setTotalOffside(teamDataall.getTotalOffside() + teamDataList.get(i).getTotalOffside());
            teamDataall.setTotalFoul(teamDataall.getTotalFoul() + teamDataList.get(i).getTotalFoul());
            teamDataall.setTotalYcard(teamDataall.getTotalYcard() + teamDataList.get(i).getTotalYcard());
            teamDataall.setTotalRcard(teamDataall.getTotalRcard() + teamDataList.get(i).getTotalRcard());
            teamDataall.setTotalPoints(teamDataall.getTotalPoints() + teamDataList.get(i).getTotalPoints());
        }

        return teamDataall;
    }


    @Override
    public Map<String, Object> getteamdatamap(Map<String,Object> teamdatamap,TeamData teamData) {
        teamdatamap.put("parameter", CollUtil.newArrayList(teamData.getTotalRounds(),teamData.getTotalWin(),teamData.getTotalFlatten(),
                teamData.getTotalLose(),teamData.getTotalGoal(),teamData.getTotalFumble(),teamData.getTotalControl(),teamData.getTotalShoot(),
                teamData.getTotalOntarget(),teamData.getTotalPass(),teamData.getTotalOkpass(),teamData.getTotalSurpass(),teamData.getTotalOffside(),
                teamData.getTotalFoul(),teamData.getTotalYcard(),teamData.getTotalRcard()));

        return teamdatamap;
    }

    @Override
    public Map<String, Object> getteamattackdatamap(Map<String, Object> teamdatamap, TeamData teamData) {
        teamdatamap.put("parameter", CollUtil.newArrayList(teamData.getTotalRounds(),teamData.getTotalWin(),teamData.getTotalFlatten(),
                teamData.getTotalLose(),teamData.getTotalGoal(),teamData.getTotalFumble(),teamData.getTotalControl(),teamData.getTotalShoot(),
                teamData.getTotalOntarget(),teamData.getTotalSurpass(),teamData.getTotalOffside(),
                teamData.getTotalFoul(),teamData.getTotalYcard(),teamData.getTotalRcard()));

        return teamdatamap;
    }

    @Override
    public Map<String, Object> getteampassdatamap(Map<String, Object> teamdatamap, TeamData teamData) {
        teamdatamap.put("parameter", CollUtil.newArrayList(teamData.getTotalPass(),teamData.getTotalOkpass()));

        return teamdatamap;
    }

    @Override
    public Map<String, Object> getteamavedatamap(Map<String, Object> teamavedatamap, TeamData teamData) {
        teamavedatamap.put("aveparameter",CollUtil.newArrayList(teamData.getAverageGoal(),teamData.getAverageFumble(),teamData.getTotalControl(),
                teamData.getAverageShoot(),teamData.getAverageOntarget(),teamData.getAveragePass(),teamData.getAverageOkpass(),
                teamData.getAverageSurpass(),teamData.getAverageOffside(), teamData.getAverageFoul(), teamData.getAverageYcard(), teamData.getAverageRcard()));

        return teamavedatamap;
    }

    @Override
    public Map<String, Object> getteamaveattackdatamap(Map<String, Object> teamavedatamap, TeamData teamData) {
        teamavedatamap.put("aveparameter",CollUtil.newArrayList(teamData.getAverageGoal(),teamData.getAverageFumble(),teamData.getTotalControl(),
                teamData.getAverageShoot(),teamData.getAverageOntarget(),
                teamData.getAverageSurpass(),teamData.getAverageOffside(), teamData.getAverageFoul(), teamData.getAverageYcard(), teamData.getAverageRcard()));

        return teamavedatamap;
    }

    @Override
    public Map<String, Object> getteamavepassdatamap(Map<String, Object> teamavedatamap, TeamData teamData) {
        teamavedatamap.put("aveparameter",CollUtil.newArrayList(teamData.getAveragePass(),teamData.getAverageOkpass()));

        return teamavedatamap;
    }


}
